package com.baosight.risk.service.system.impl;

import com.baosight.risk.api.xtzy.IXtsqMenuAuthoService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.service.system.entity.SysMenu;
import com.baosight.risk.mapper.system.SysMenuMapper;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.api.system.ISysMenuService;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.List;

/**
 * 菜单 业务层处理
 *
 * @author duanyongqiang
 * @date 2019.3.27
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private IXtsqMenuAuthoService authoService;

    @Override
    public BaseMapper getMapper() {
        return menuMapper;
    }

    /**
     * 根据用户查询菜单
     *
     * @param user 用户信息
     * @return 菜单列表
     */
    @Override
    public List<Map<String, Object>> selectMenusByUser(XtzyUser user) {
        List<SysMenu> menus;
        // 管理员显示所有菜单信息
        if (user.isAdmin()) {
            menus = menuMapper.selectMenuNormalAll();
//            menus = menuMapper.selectAll();
        } else {
//            menus = menuMapper.selectMenusByUserId(user.getId());
            menus = authoService.getMenuByCompAndUser(user.getExt1(), user);
        }
        return getTreeChildPerms(menus, "0");
    }

    /**
     * 根据用户ID查询菜单
     *
     * @param user 用户信息
     * @return 菜单列表
     */
    @Override
    public List<Map<String, Object>> selectMenusByCompCode(XtzyUser user) {
        List<SysMenu> menus;
        // 管理员显示所有菜单信息
        if (user.isAdmin()) {
            menus = menuMapper.selectMenuNormalAll();
        } else {
            //根据用户选择的组织去查询菜单
            menus = menuMapper.selectMenusByCompCode(user.getExt1(), user.getId());
        }
        return getTreeChildPerms(menus, "0");
    }

    /**
     * R
     * 查询菜单集合
     *
     * @return 所有菜单信息
     */
    @Override
    public List<SysMenu> selectMenuList(SysMenu menu) {
        List<SysMenu> list = menuMapper.selectMenuList(menu);
        return list;
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectPermsByUserId(String userId) {
        List<String> perms = menuMapper.selectPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }


    /**
     * 删除菜单管理信息
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuById(String menuId) {
        int result = menuMapper.deleteMenuById(menuId);
        return result;
    }

    /**
     * 根据菜单ID查询信息
     *
     * @param menuId 菜单ID
     * @return 菜单信息
     */
    @Override
    public SysMenu selectMenuById(String menuId) {
        return menuMapper.selectMenuById(menuId);
    }

    /**
     * 查询子菜单数量
     *
     * @param parentId 菜单ID
     * @return 结果
     */
    @Override
    public int selectCountMenuByParentId(String parentId) {
        return menuMapper.selectCountMenuByParentId(parentId);
    }

    /**
     * 查询菜单使用数量
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int selectCountRoleMenuByMenuId(String menuId) {
        return menuMapper.selectCountRoleMenuByMenuId(menuId);
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return List<Map < String, Object>>
     */
    public List<Map<String, Object>> getTreeChildPerms(List<SysMenu> list, String parentId) {
        List<Map<String, Object>> returnList = new ArrayList<>();
        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
            SysMenu menu = iterator.next();
            Map<String, Object> map = mapObejct(menu);
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (menu.getParentId().equals(parentId)) {
                recursionTreeFn(list, map);
                returnList.add(map);
            }
        }
        return returnList;
    }

    /**
     * 封装tree 类型
     *
     * @param menu
     * @return
     */
    private Map<String, Object> mapObejct(SysMenu menu) {
        Map<String, Object> deptMap = new HashMap<>();
        deptMap.put("id", menu.getId());
        deptMap.put("iconCls", menu.getIcon());
        deptMap.put("text", menu.getMenuName());
        deptMap.put("route", menu.getRoute());
        deptMap.put("link", menu.getOutLink());
        deptMap.put("code", menu.getEnCode());
        deptMap.put("auth", menu.getExt5());
        deptMap.put("type", menu.getMenuType());
        deptMap.put("key", menu.getMenuKey());
        return deptMap;
    }

    /**
     * 递归列表
     */
    private void recursionTreeFn(List<SysMenu> list, Map<String, Object> map) {
        // 得到子节点列表
        List<Map<String, Object>> childList = getTreeChildList(list, map.get("id").toString());
        if (hasChild(list, map.get("id").toString())) {
            map.put("children", childList);
        }
        for (Map tChild : childList) {
            if (hasChild(list, tChild.get("id").toString())) {
                // 判断是否有子节点
                Iterator<Map<String, Object>> it = childList.iterator();
                while (it.hasNext()) {
                    recursionTreeFn(list, it.next());
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Map<String, Object>> getTreeChildList(List<SysMenu> list, String id) {
        List<Map<String, Object>> tlist = new ArrayList<>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext()) {
            SysMenu n = it.next();
            if (id.equals(n.getParentId())) {
                Map<String, Object> map = mapObejct(n);
                map.put("url", n.getUrl());
                tlist.add(map);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysMenu> list, String id) {
        return getTreeChildList(list, id).size() > 0 ? true : false;
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
        List<SysMenu> returnList = new ArrayList<>();
        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
            SysMenu t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId().equals(String.valueOf(parentId))) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysMenu> list, SysMenu t) {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        if (hasChild(list, t.getId())) {
            t.setChildren(childList);
        }
        for (SysMenu tChild : childList) {
            if (hasChild(list, tChild.getId())) {
                // 判断是否有子节点
                Iterator<SysMenu> it = childList.iterator();
                while (it.hasNext()) {
                    SysMenu n = it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
        List<SysMenu> tlist = new ArrayList<>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext()) {
            SysMenu n = it.next();
            if (t.getId().equals(n.getParentId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

}
